package org.example.config;

import org.aspectj.lang.annotation.Aspect;
import org.example.async.ArtisanThreadPoolTaskExecutor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.Executor;

@Configuration
@EnableAsync
public class ThreadPoolConfig {

    @Bean("asyncExecutor")
    public Executor executor() {
        final int cpuSize = Runtime.getRuntime().availableProcessors();
        ArtisanThreadPoolTaskExecutor executor = new ArtisanThreadPoolTaskExecutor();
        executor.setCorePoolSize(cpuSize);
        executor.setMaxPoolSize(2 * cpuSize);
        executor.setQueueCapacity(500);
        executor.setKeepAliveSeconds(60);
        executor.setThreadNamePrefix("asyncArtisan-");
        executor.initialize();
        return executor;
    }
}
